package com.power.sifa.correction.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.plf.common.domain.PageDataResponse;
import com.plf.core.common.service.BaseServiceImpl;
import com.power.sifa.correction.dao.Cre_workDao;
import com.power.sifa.correction.dao.Cre_work_recordDao;
import com.power.sifa.correction.model.Cre_work_record;
import com.power.sifa.correction.model.pageInfo.Cre_work_recordPageInfo;
import com.power.sifa.correction.service.Cre_work_recordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * 处置记录业务层实现
 */
@Service
public class Cre_work_recordServiceImpl extends BaseServiceImpl implements Cre_work_recordService {

    @Resource
    private Cre_work_recordDao dao;

    @Override
    public Cre_work_record getById(Serializable id) {
        return dao.findById(id).orElse(null);
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void saveOrUpdate(Cre_work_record cre_work_record) {
        // save 如果数据库不存在这条数据，就会执行保存（即将这条数据插入到数据库）；如果数据库存在这条数据，就会根据主键去更新这条数据
        if (cre_work_record.getPrimaryKey() != null) {
            Cre_work_record cre_work_recordfDb = dao.findById(cre_work_record.getPrimaryKey()).orElse(null);
            if (cre_work_recordfDb != null) {
                BeanUtils.copyProperties(cre_work_record, cre_work_recordfDb);
                dao.save(cre_work_recordfDb);
                return;
            }
        }
        dao.save(cre_work_record);
    }

    @Override
    public void removeById(Serializable id) {
        dao.deleteById(id);
    }

    @Override
    public Page<Cre_work_record> findAll(Cre_work_recordPageInfo pageInfo) {
        //排序等定义
        Sort sort;
        if ("asc".equals(pageInfo.getOrder())) {
            sort = Sort.by(Sort.Direction.ASC, pageInfo.getSort());
        } else {
            sort = Sort.by(Sort.Direction.DESC, pageInfo.getSort());
        }
        //分页的定义
        Pageable pageable = PageRequest.of(pageInfo.getPageNumber() <= 1 ? 0 : pageInfo.getPageNumber() - 1, pageInfo.getPageSize(), sort);
        //查询条件
        Specification spec = this.createSpecification(pageInfo);
        return dao.findAll(spec, pageable);
    }

    @Transactional(rollbackFor = Exception.class)
    public void removeByIds(List<Integer> ids) {
        dao.removeByIds(ids);
    }

    @Override
    public List<Cre_work_record> findAll() {
        return dao.findAll();
    }

    @Override
    public void saveAll(List<Cre_work_record> list) {
        dao.saveAll(list);
    }

    public PageDataResponse queryList(Cre_work_recordPageInfo pageInfo) {
        List<Cre_work_record> list = this.dao.queryList(pageInfo);
        // 分页
        PageDataResponse page = new PageDataResponse();
        if (CollectionUtil.isNotEmpty(list)) {
            Long total = Long.valueOf(pageInfo.getAllcount());
            int size = pageInfo.getPageSize() * pageInfo.getPageNumber();
            if (total.intValue() - size > 0) {
                page.setNextPage(pageInfo.getPageNumber() + 1);
            }
            page.setSize(pageInfo.getPageSize());
            page.setCurrent(pageInfo.getPageNumber());
            page.setTotal(total);
        } else {
            page.setSize(0);
            page.setCurrent(1);
            page.setTotal(0L);
        }
        page.setRecords(list);
        return page;
    }

    @Override
    public List<Cre_work_record> findListByWarnId(String warnid) {
        return this.dao.findListByWarnId(warnid);
    }
}
